package com.zzq.base.awesome.code;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.zzq.base.awesome.api.entity.User;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class TsCodeGenetate {

	private static Map<String, String> map = new HashMap<>();
	static {
		map.put("String", "string");
		map.put("double", "number");
		map.put("int", "number");
		map.put("long", "number");
		map.put("float", "number");
		map.put("Boolean", "boolean");
		map.put("Float", "number");
		map.put("Integer", "number");
		map.put("List", "Array<>");
		map.put("LocalDateTime", "string");
	}

	public static String gentrateInterface(Class c) {
		if (c.isAnnotationPresent(ApiModel.class)){
			ApiModel a = (ApiModel) c.getAnnotation(ApiModel.class);
			System.out.println("// " + a.description());
		}
		System.out.println("export interface " + c.getSimpleName() + " {");
		Field[] fields = c.getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			if (fields[i].isAnnotationPresent(JsonIgnore.class)) {
				continue;
			}
			if ("serialVersionUID".equals(fields[i].getName())) {
				continue;
			}
			System.out.print("\t");
			System.out.print(fields[i].getName());
			System.out.print(": ");
			System.out.print(map.getOrDefault(fields[i].getType().getSimpleName(),
					fields[i].getType().getSimpleName()));
			System.out.print(";");
			if (fields[i].isAnnotationPresent(ApiModelProperty.class)){
				ApiModelProperty x = (ApiModelProperty) fields[i].getAnnotation(ApiModelProperty.class);
				System.out.println("\t// " + x.value());
			}
		}
		System.out.println("}");
		return "";
	};
	public static String gentrateClass(Class c) {
		if (c.isAnnotationPresent(ApiModel.class)){
			ApiModel a = (ApiModel) c.getAnnotation(ApiModel.class);
			System.out.println("// " + a.description());
		}
		System.out.println("export class " + c.getSimpleName() + " {");
		System.out.println("\tconstructor(");
		Field[] fields = c.getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			if (fields[i].isAnnotationPresent(JsonIgnore.class)) {
				continue;
			}
			if ("serialVersionUID".equals(fields[i].getName())) {
				continue;
			}
			System.out.print("\tpublic ");
			System.out.print(fields[i].getName());
			System.out.print(": ");
			System.out.print(map.getOrDefault(fields[i].getType().getSimpleName(),
					fields[i].getType().getSimpleName()));
			if (i != fields.length - 1) {
				System.out.print(",");
			}
			if (fields[i].isAnnotationPresent(ApiModelProperty.class)) {
				ApiModelProperty x = (ApiModelProperty) fields[i].getAnnotation(ApiModelProperty.class);
				System.out.println("\t// " + x.value());
			}
		}
		System.out.println("\t) {");
		System.out.println("\t}");
		System.out.println("}");
		return "";
	};
	public static void main(String[] args) {
		gentrateClass(User.class);
		gentrateInterface(User.class);
	}
}
